package com.qfedu.edu.utils;

import com.alibaba.fastjson.JSON;
import com.qfedu.edu.result.R;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xiaobobo
 * @title: HttpResponseUtils
 * @projectName cd-fy-2401-third-project-parent
 * @description: 这个是请求响应的这个工具类
 * @date 2024/9/13  14:54
 */
public class HttpResponseUtils {
    /**
     * 向客户端响应消息
     *
     * @param resp
     * @param r
     */
    public static void sendResponse(HttpServletResponse resp, R r) {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            writer.write(JSON.toJSONString(r));
        } catch (IOException e) {
            throw new RuntimeException("向客户端写数据出错....");
        } finally {
            writer.flush();
            writer.close();
        }

    }

}
